<#include "/templates/taglibs.ftl" />
<#include "/templates/function.ftl" />
<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="utf-8">
        <link rel="stylesheet" href="${contextPath}/assets/plugins/layui/css/layui.css" media="all" />
        <style>
            p {
                margin-bottom: 10px;
                font-size: 15px;
                color: #393D49;
                font-weight: 400;
                line-height: 1.8;
            }
            .site-content {
                background-color: #fff;
                padding: 20px 0 10px 20px;
            }
            .site-h1 {
                margin-bottom: 20px;
                line-height: 60px;
                padding-bottom: 10px;
                color: #393D49;
                border-bottom: 1px solid #eee;
                font-size: 28px;
                font-weight: 400;
            }
        </style>
    </head>
    <body>
        <div class="layui-fluid layui-container">
            <div class="layui-row site-content">
                <div class="layui-col-md12">
                    <h1 class="site-h1">数据层Dao</h1>
                    <p>数据层，在项目中，一般会实现很多的单一的功能。</p>
                    <p>在项目中写数据层一般遵循下面的规范：</p>
                    <p>1.只实现一个数据库实体类的操作。</p>
                    <p>2.命名：接口：实体类名+Dao，实现类：实体类名+DaoImpl，例如：UserDao，UserDaoImpl。</p>
                    <p>3.接口应该继承BaseDao，实现类继承于BaseDaoImpl。BaseDao是泛型类，实现了数据层常用的基本方法。</p>
                    <p>4.通过继承BaseDao，增加或重写业务所需的方法。</p>
                    <p>5.数据库的操作，建议都使用Hql语句完成，不使用或尽量少使用Sql语句实现。</p>
                    <p>6.数据库的查询语句，禁止出现 * ,例如： select * from tablename,要列出具体的字段名称，select T.uid,T.uname from users T;</p>
                    <p>7.数据层在实现类上增加注解@Repository，使该类定义为Dao组件类，例如：@Repository(“BaseDao”)</p>
                    <fieldset class="layui-elem-field layui-field-title site-title">
                        <legend><a name="get">示例代码</a></legend> 
                    </fieldset>
                    <pre class="layui-code" lay-skin="notepad">
package org.stvd.repository.admin;
import java.util.List;
import org.stvd.core.dto.QueryResult;
import org.stvd.entities.admin.Users;
import org.stvd.repository.base.BaseDao;
public interface UsersDao extends BaseDao<Users> {
    List<Users> findUsersById(String userid);
    QueryResult<Users> getUsersQueryResult(int start, int limit, String userid, String uname,
            List<String> dep_ids, String roleid);
    List<Users> findUsers(String userid, String dep_id, String dep_type);
}
                    </pre>
                    <pre class="layui-code" lay-skin="notepad">
package org.stvd.repository.admin.impl;
import java.util.List;
import org.springframework.stereotype.Repository;
import org.stvd.core.dto.QueryResult;
import org.stvd.entities.admin.Users;
import org.stvd.repository.admin.UsersDao;
import org.stvd.repository.base.impl.BaseDaoImpl;
@Repository("UsersDao")
public class UsersDaoImpl extends BaseDaoImpl<Users> implements UsersDao {
    @Override
    public List<Users> findUsersById(String userid) {
        return findByHQL("from Users T where T.userid= ?0", userid);
    }
    @Override
    public QueryResult<Users> getUsersQueryResult(int start, int limit, String userid, String uname,
            List<String> dep_ids, String roleid) {
        String hql = "select T1 from Users T1 " + "where (T1.userid = ?0 or '' = ?0) "
                + "and (T1.uname like ?1 or '' = ?1) "
                + "and exists(select T2.dep_id from UserDep T2,Department T3 where T2.dep_id=T3.dep_id "
                + "and T2.userid=T1.userid and T3.dep_id in (?2)) "
                + "and (?3 = '' or exists(select T4.userid from UserRole T4,Roles T5 where T4.roleid=T5.roleid "
                + "and T4.userid=T1.userid and T5.roleid = ?3) ) ";
        return getQueryResultByHQL(start, limit, hql, userid, "%" + uname + "%", dep_ids, roleid);
    }
    @Override
    /**
     * 根据userid、dep_id、机构类型查询
     */
    public List<Users> findUsers(String userid, String dep_id, String dep_type) {
        String hql = "select T1 from Users T1 " + "where (T1.userid = ?0 or '' = ?0) "
                + "and exists(select T2.dep_id from UserDep T2,Department T3 where T2.dep_id=T3.dep_id "
                + "and T2.userid=T1.userid and ('' = ?1 or T3.dep_id = ?1) and ('' = ?2 or T3.dep_type= ?2)) ";
        return this.findByHQL(hql, userid, dep_id, dep_type);
    }
}
                    </pre>
                </div>
            </div>
        </div>
    </body>
    <script type="text/javascript" src="${contextPath}/assets/plugins/layui/layui.js"></script>
    <script>
        layui.use('code', function(){ 
            layui.code({
                encode: true
            }); 
          });
    </script>
</html>